package com.hoapital.outpbillwork.module.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoapital.outpbillwork.module.entity.ExamAppoints;
import com.hoapital.outpbillwork.module.entity.ExamItems;
import com.hoapital.outpbillwork.module.entity.ExamMaster;
import com.hoapital.outpbillwork.module.mapper.ExamAppointsMapper;
import com.hoapital.outpbillwork.module.mapper.ExamItemsMapper;
import com.hoapital.outpbillwork.module.mapper.ExamMasterMapper;
import com.hoapital.outpbillwork.module.mapper.OutpDoctorAssistantMapper;
import com.hoapital.outpbillwork.module.service.DrugPrescMasterService;
import com.hoapital.outpbillwork.module.service.ExamMasterService;
import com.hospital.common.core.exception.ExceptionHelp;
import com.hospital.common.core.exception.ExceptionHelpEnum;
import com.hospital.common.core.module.controller.vo.Receive;
import com.hospital.common.core.module.controller.vo.ReceiveDate;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lvyao
 * @since 2021-02-25
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ExamMasterServiceImpl extends ServiceImpl<ExamMasterMapper, ExamMaster> implements ExamMasterService {

    @Resource
    private ExamItemsMapper examItemsMapper;

    @Resource
    private ExamMasterMapper examMasterMapper;

    @Resource
    private ExamAppointsMapper examAppointsMapper;

    @Resource
    private DrugPrescMasterService drugPrescMasterService;

    @Resource
    private OutpDoctorAssistantMapper outpDoctorAssistantMapper;

    @Override
    public boolean saveBillExam(Receive receive) throws ParseException {
        String examNo = receive.getApplyNo() == null || "".equals(receive.getApplyNo()) ? outpDoctorAssistantMapper.getApplyNo(receive.getPrimaryDate(), receive.getPrimaryNo(), "1") : receive.getApplyNo();
        List<ReceiveDate> receiveDateList = receive.getData();
        if (examNo != null && examNo != "" && receiveDateList != null && !receiveDateList.isEmpty()) {
            List<ExamItems> examItemsList = examItemsMapper.selectList(new QueryWrapper<ExamItems>().eq("exam_no", examNo));
            if (examItemsList != null && !examItemsList.isEmpty()) {
                ExamAppoints examAppoints = examAppointsMapper.selectById(examNo);
                //所有项目收费标记都完成了再删除预约表数据插入检查主表数据
                ExamMaster examMaster = new ExamMaster();
                BeanUtils.copyProperties(examAppoints, examMaster);
                if (examMasterMapper.insert(examMaster) > 0) {
                    if (examAppointsMapper.delete(new QueryWrapper<ExamAppoints>().eq("exam_no", examNo)) <= 0) {
                        throw new ExceptionHelp(ExceptionHelpEnum.DELETE_REQ_INFO);
                    }
                } else {
                    throw new ExceptionHelp(ExceptionHelpEnum.FILED_INSERT_EXAM_MASTER);
                }
                //检查主索引考虑增加，因申请中暂未给出所以暂不考虑，这两个参数值在确认费用时才在复诊扣费产生
//                List<ExamPatMi> examPatMiList = examPatMiMapper.selectList(new EntityWrapper<ExamPatMi>().eq("LOCAL_ID_CLASS", examAppoints.getLocalIdClass()).eq("PATIENT_LOCAL_ID", examAppoints.getPatientLocalId()));
//                if (examPatMiList == null|| examPatMiList.isEmpty()){
//                    ExamPatMi examPatMi = new ExamPatMi();
//                    BeanUtils.copyProperties(examAppoints, examPatMi);
//                    if (examPatMiMapper.insert(examPatMi) <= 0){
//                        throw  new ExceptionHelp(ExceptionHelpEnum.INSERT_EXAM_PAT_MI);
//                    }
//                }
                //处理门诊医嘱主表，删除门诊医嘱临时表，删除门诊计费临时表，插入住院计费，插入门诊计费表 --新增判断若无任务分类按集中模式处理标记
                if (!(receive.getTaskId() == null) && !receive.getTaskId().equals("")) {
                    boolean commBill = drugPrescMasterService.commBill(receive, examAppoints.getPatientId(), String.valueOf(examAppoints.getVisitId()), examAppoints.getReqDept());
                    if (!commBill) {
                        throw new ExceptionHelp(ExceptionHelpEnum.COMMBILL);
                    }
                }
                return true;
            }
        } else {
            throw new ExceptionHelp(ExceptionHelpEnum.EXAM_BILL_ITEM);
        }
        return false;
    }
}
